<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 方式一：通过父传子进行控制是否显示 -->
      <!-- <PageTool :show-before="false"> -->
      <!-- 方式二：子组件通过$slots自行判断，控制子组件插槽 -->
      <PageTool>
        <el-button
          slot="after"
          type="primary"
          size="smail"
          @click="onShowDialog()"
        >新增权限</el-button>
      </PageTool>
      <el-table :data="list" border row-key="id">
        <el-table-column label="名称" prop="name" align="" />
        <el-table-column label="标识" prop="code" align="center" />
        <el-table-column label="描述" prop="description" align="center" />
        <el-table-column label="操作" prop="" align="center">
          <template v-slot="{ row }">
            <el-button type="text" @click="onEidt(row.id)">编辑</el-button>
            <el-button
              type="text"
              @click="onShowDialog({ pid: row.id, type: row.type + 1 })"
            >添加</el-button>
            <el-button type="text" @click="onDel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <AddPerm v-if="showDialog" ref="addPermRef" :title="title" :visible="showDialog" @close="showDialog = false" /></div>
</template>

<script>
import { getPermissionList, delPermission, getPermissionDetail } from '@/api/permisson'
import { translateListToTree } from '@/utils'
import AddPerm from './components/AddPerm.vue'
export default {
  name: 'Permission',
  components: { AddPerm },
  props: {},
  data() {
    return {
      list: [],
      showDialog: false,
      title: ''
    }
  },
  created() {
    this.getList()
  },
  mounted() {},
  methods: {
    async onEidt(id) {
      this.title = '编辑权限'
      const form = await getPermissionDetail(id)
      console.log(form)
      this.showDialog = true
      if (form.enVisible === '0') {
        form.enVisible = false
      } else if (form.enVisible === '1') {
        form.enVisible = true
      }
      this.$nextTick(() => {
        Object.assign(this.$refs.addPermRef.form, form)
      })
    },
    onShowDialog(node) {
      this.title = '新增权限'
      this.showDialog = true
      this.$nextTick(() => {
        Object.assign(this.$refs.addPermRef.form, node)
      })
    },
    async getList() {
      this.list = translateListToTree(await getPermissionList(), '0')
    },
    onDel(id) {
      this.$confirm('确认删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await delPermission(id)
          this.$message.success('操作成功')
          this.getList()
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped></style>
